<template>
  <a-modal 
    :title="title" 
    :width="800" 
    :visible="visible" 
    :confirm-loading="confirmLoading" 
    cancel-text="关闭" 
    @ok="submit" 
    @cancel="closeModal"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="昵称">
          <a-input v-decorator="['nickname', validatorRules.nickname ]" placeholder="请输入昵称" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="手机号">
          <a-input v-decorator="['mobile', validatorRules.mobile ]" placeholder="请输入手机号" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="姓名">
          <a-input v-decorator="['name', validatorRules.name ]" placeholder="请输入姓名" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import ApiTree from '@/api/personnel/memberList';
import { Slime } from '@/mixins/slime';
export default {
  name: 'memberListModal',
  data() {
    return {
      title: '编辑',
      form: this.$form.createForm(this),
      model: {},
      validatorRules: {
        nickname: { rules: [{ required: true, message: '请输入昵称!' }] },
        mobile: { rules: [{ required: true, message: '请输入电话!' }] },
        name: { rules: [{ required: true, message: '请输入姓名!' }] },
      },
      id: 0,
    };
  },
  mixins: [Slime],
  methods: {
    // 增
    crudCreate() {
      this.crudUpdate({});
    },
    // 改
    crudUpdate(record) {
      this.form.resetFields(); //重新设置领域
      this.visible = true;
      if(!record.id){
        return false
      }
      ApiTree.getDetail(record.id)
        .then((res) => {
          let data = res.result
          this.id = data.id;
          this.form.setFieldsValue({
            nickname: data.nickname,
            mobile: data.mobile,
            name: data.name,
          });
        })
        .finally(() => {
        });
    },
    // 提交
    submit() {
      const that = this;
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let param = values
          if (!that.id) {
            // that.submitCreate(param);
          } else {
            param = {...param, id:that.id}
            that.submitUpdate(param);
          }
        }
      });
    },
    // 提交 增
    // submitCreate(param) {
    //   const that = this;
    //   ApiTree.getAdd(param)
    //     .then((res) => {
    //       that.$message.success('新增成功');
    //     })
    //     .finally(() => {
    //       that.closeModal();
    //     });
    // },
    // 提交 改
    submitUpdate(param) {
      const that = this;
      ApiTree.getUpdate(param)
        .then((res) => {
          that.$message.success('编辑成功');
        })
        .finally(() => {
          that.closeModal();
        });
    },
    // 弹窗-关闭
    closeModal() {
      this.confirmLoading = false;
      this.visible = false;
      this.id = null
      this.form.resetFields();
      this.$emit('closeModal');
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>